package com.libre.common.utils;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.libre.common.vo.PageVo;

import java.util.ArrayList;
import java.util.List;

/**
 * @description 分页工具类
 * @author 庞留杰
 * @date 2020/7/16 15:13
 * @version V1.0
 **/
public class PageUtil {

    /**
    * @description   Mybatis-Plus分页封装
    * @author        庞留杰
    * @date          2020/7/22 18:07
    * @version       V1.0
    **/
    public static Page initMpPage(PageVo page) {

        Page p = null;
        int pageNumber = page.getPageNumber();
        int pageSize = page.getPageSize();
        String sort = page.getSortField();
        String order = page.getOrderType();
        if (pageNumber < 1) {
            pageNumber = 1;
        }
        if (pageSize < 1) {
            pageSize = 10;
        }
        if (StringUtil.isNotEmpty(sort)) {
            Boolean isAsc = false;
            if (!StringUtil.isNotEmpty(order)) {
                isAsc = false;
            } else {
                if ("desc".equals(order.toLowerCase())) {
                    isAsc = false;
                } else if ("asc".equals(order.toLowerCase())) {
                    isAsc = true;
                }
            }
            p = new Page(pageNumber, pageSize);
            /* 旧版本3.1,新版已经废弃
                if (isAsc) {
                    p.setAsc(sort);
                } else {
                    p.setDesc(sort);
                }
            */
            List<OrderItem> list = new ArrayList<>();
            OrderItem orderItem = new OrderItem();
            orderItem.setAsc(isAsc);
            orderItem.setColumn(sort);
            list.add(orderItem);
            p.setOrders(list);
        } else {
            p = new Page(pageNumber, pageSize);
        }
        return p;
    }
}
